package com.aspyn.notiftest;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class NotificationsTestActivity extends Activity {
	/** Called when the activity is first created. */
	
	private static final int NOTIFY_ME_ID = 1337;
	private int count = 0;
	private Timer timer = new Timer();
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button btn = (Button)findViewById(R.id.btnStart);
        btn.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				TimerTask task = new TimerTask() {
					public void run() {
						notifyMe();
					}
				};
				timer.schedule(task, 0, 5000);
			}
		
        });
        Button btn2 = (Button)findViewById(R.id.btnStop);
        btn2.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				NotificationManager mgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
				mgr.cancel(NOTIFY_ME_ID);
				timer.cancel();
			}
        	
        });
    }
   
    
    public void notifyMe() {
    	final NotificationManager mgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    	Notification note = new Notification(R.drawable.ic_launcher,"Status msg",System.currentTimeMillis());
    	
    	PendingIntent i = PendingIntent.getActivity(this, 0, new Intent(this, NotificationsTestActivity.class), 0);
    	note.setLatestEventInfo(this, "Notif Title", "Notif Msg", i);
    	
    	note.number=count++;
    	mgr.notify(NOTIFY_ME_ID, note);
    }
}